let tableIns;
let tree;
layui.use(['table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    tree = layui.tree;
    let laydate = layui.laydate;
    let form = layui.form;
    let height = document.documentElement.clientHeight - 60;
    let _curStr = window.location.href.split("?")[1];

    tableIns = table.render({
        elem: '#balanceTable'
        , url: '/seller/qryBalance/detail'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , where:{
            sellerId:_curStr
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            console.log(data.length);
            var shouru = 0;
            var zhichu = 0;
            for(i=0;i<data.rows.length;i++){
            	if(data.rows[i].operType == '1') {
            		shouru = shouru + data.rows[i].tradeMoney
            	}else{
            		zhichu = zhichu + data.rows[i].tradeMoney
            	}
            }
            $("#sum_money1").html("收入总金额："+formatMoney(shouru));
            $("#sum_money2").html("支出总金额："+formatMoney(zhichu));
            // console.log("data=="+data);
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , toolbar: true
        , title: '商户余额明细'
        , cols: [[
            {field: 'tradeSerial', title: '交易流水号', sort: true}
            , {field: 'tradeComment',title: '支付项目', sort: true}
            , {field: 'tradeMoney', title: '支付金额',align:'right', sort: true , templet:function (d) {
            	if(d.operType == '1') {
            			return '<span style="color: green"> + ' + formatMoney(d.tradeMoney) + '元</span>';
            	}else{
            		return '<span style="color: red"> - ' + formatMoney(d.tradeMoney) + '元</span>';
            	}
                }}
            , {field: 'cpmTime', title: '结算时间', sort: true}
            , {field: 'payTime', title: '交易时间', sort: true}
        ]]
        , defaultToolbar: ['', '', '']
        , page: false
        , limit: 20000 //默认每页多少条
        , height: height
        , cellMinWidth: 80
    });

});

function formatMoney(str) {
    if (!str) str = '0.00';
    let ret = Math.round(parseFloat(str) * 100) / 100;
    let decimal = ret.toString().split('.');
    if (decimal.length === 1) {
        return ret.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            + '.00';
    };
    if (decimal.length > 1) {
        decimal[0] = decimal[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        if (decimal[1].length < 2) {
            decimal[1] += '0';
            return decimal.join(".");
        }
        return decimal.join(".");
    };
    return ret;
}

